const urlParams = new URLSearchParams(window.location.search);
const id = urlParams.get('id');
let initTitle=''
new Vue({
    el:'#newsUpdate',
    data:{
        id:id,
        title:'',
        content:'',
        delFlag:0,
        infoFlag:false,
        infoText:''
    },
    methods:{
        modifyNews()
        {
            if(this.title==='')
            {
                this.$refs.span_title.innerHTML="标题不能为空"
                this.$refs.span_title.style.color="red"
                this.$refs.span_title.style.fontSize="12px"
                return false
            }
            if(this.content==='')
            {
                this.$refs.span_content.innerHTML="内容不能为空"
                this.$refs.span_content.style.color="red"
                this.$refs.span_content.style.fontSize="12px"
                return false
            }
            if(confirm("确认保存修改吗？"))
            {
                axios({
                    url:'/api/updateNews',
                    params:{
                       id,
                       title:this.title,
                       content:this.content,
                       delFlag:this.delFlag
                    }
                }).then(rs=>{
                    if(rs.data.updateMsg==="success")
                    {
                        this.infoFlag=true
                        this.infoText='修改成功'

                    }else{
                        this.infoFlag=true
                        this.infoText="修改失败"
                    }
                })
            }
        },
        getNewsById()
        {
            axios.get('/api/getNewsById',
                {
                    params:{
                        id:parseInt(this.id)
                    }
                }).then(rs=>{
                    console.log(rs.data)
                    this.id=rs.data.news.id
                    this.title=rs.data.news.title
                    this.content=rs.data.news.content
                    this.delFlag=rs.data.news.delFlag
                    initTitle=rs.data.news.title
            })
        },
        checkTitle()
        {
            axios.get('/api/checkTitle',
                {
                    params:{
                        title:this.title
                    }
                }).then(rs=>{
                console.log(this.$refs.span_title)

                if(rs.data.isCheck==="yes"||this.title==='')
                {
                    this.$refs.add.disabled=true
                    this.$refs.span_title.innerHTML="标题已存在"
                    this.$refs.span_title.style.color="red"
                    this.$refs.span_title.style.fontSize="12px"
                }
                if(this.title===initTitle||rs.data.isCheck==="no"){
                    this.$refs.add.disabled=false
                    this.$refs.span_title.innerHTML="标题可用"
                    this.$refs.span_title.style.color="green"
                    this.$refs.span_title.style.fontSize="12px"
                }
                console.log(initTitle)
            })
        },
        backList()
        {
            window.location='/api/newsList.html'
        },
        closeInfo()
        {
            this.infoFlag=false
            window.location="/api/newsList.html"
        }
    },
    created(){
        this.getNewsById()
    }
})